package com.redoor.app.stake.workflow.timetask;

import com.redoor.app.stake.basicdata.model.RegistrationApplyForRedis;
import com.redoor.app.stake.basicdata.model.RegistrationForRedisEnum;
import com.redoor.app.stake.basicdata.service.BaseRegistrationApplyService;
import com.redoor.app.stake.basicdata.service.RegistrationApplyRedisService;
import com.redoor.app.stake.workflow.service.RegistrationApplyWorkOrderService;
import com.redoor.app.stake.workflow.service.WorkOrderService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;

import java.util.List;

/**  
 *  定时取redis里面的注册码申请信息,批量处理  
 *   
 * @author ZhangMumu  
 * @date 2018/4/3 0003 14:52
 */
@Configuration
@EnableScheduling
public class RegistrationApplyScheduling {

    private final Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    private RegistrationApplyRedisService registrationApplyRedisService;
    @Autowired
    BaseRegistrationApplyService registrationApplyService;
    @Autowired
    WorkOrderService workOrderService;

    /**
     *  定时从redis里取注册码申请信息处理
     *
     * @author ZhangMumu
     * @date 2018/4/3 0003 15:04
     * @param
     * @return
     */
    @Scheduled(fixedRate = 10000)
    public void dealLogIntoDataBase() {
        try {
            /*从redis里取十条注册码申请信息并处理*/
            List<RegistrationApplyForRedis> regisAppList = registrationApplyRedisService.getApplyInfoFromRedis(10,RegistrationForRedisEnum.REGISTRATION_FOR_REDIS);
            if (null != regisAppList && 0 < regisAppList.size()){
                /*保存注册码申请信息*/
                int result = registrationApplyService.saveAll(regisAppList);
                if (0 < result){
                    /*生成对应的注册码审核工单*/
                    result = workOrderService.reApplySaveAll(regisAppList);
                    if (0 < result){
                        logger.info("deal_registration_info_success");
                    }
                }
            }
        } catch (Exception e) {
            logger.error(e.getLocalizedMessage());
        }
    }
}
